
// UIEditor_CPPView.cpp : implementation of the CUIEditor_CPPView class
//

#include "stdafx.h"
#include "UIEditor_CPP.h"

#include "UIEditor_CPPDoc.h"
#include "UIEditor_CPPView.h"

#include <gdiplus.h>
#include "control_factory.h"
using namespace Gdiplus;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CUIEditor_CPPView

IMPLEMENT_DYNCREATE(CUIEditor_CPPView, CView)

BEGIN_MESSAGE_MAP(CUIEditor_CPPView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CUIEditor_CPPView::OnFilePrintPreview)
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CUIEditor_CPPView construction/destruction

CUIEditor_CPPView::CUIEditor_CPPView()
	:m_backbuffer(NULL)
	,m_backbufferGraphics(NULL)
{
	// TODO: add construction code here

	m_window = new window;
	m_Tester = new control();
	m_Tester->m_padding = rect(6,6,6,6);
	m_Tester->set_rect(rect(20, 20, 400, 400));


	control* child = control_factory::alloc_control(L"control");
	child->m_align = align_x2;
	child->set_rect(rect(10, 10, 100, 100));
	m_Tester->push_back(child);

	child = control_factory::alloc_control(L"image_button");
	child->m_align = align_x1;
	child->set_rect(rect(0, 0, 100, 100));
	m_Tester->push_back(child);

	child = new image_button;
	child->m_align = align_y2;
	child->set_rect(rect(0, 0, 100, 100));
	m_Tester->push_back(child);

	child = new image_button;
	child->m_align = align_xy;
	child->m_margin = rect(5, 5, 5, 5);
	child->set_rect(rect(0, 0, 100, 100));
	m_Tester->push_back(child);

	m_Tester->align();

	m_window->set_ctl_tree(m_Tester);	
}

CUIEditor_CPPView::~CUIEditor_CPPView()
{
}

BOOL CUIEditor_CPPView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

// CUIEditor_CPPView drawing

void CUIEditor_CPPView::OnDraw(CDC* pDC)
{
	CUIEditor_CPPDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	//CMFCPropertySheet

	CRect rt;
	GetClientRect(&rt);
	if (m_backbuffer == NULL || m_backbufferGraphics == NULL)
	{		
#pragma push_macro("new")
#undef new
		m_backbuffer = new Bitmap(rt.Width(), rt.Height());
#pragma pop_macro("new")
		m_backbufferGraphics = Graphics::FromImage(m_backbuffer);
	}
	Color color(110, 100, 100);
	SolidBrush brs(color);
	m_backbufferGraphics->FillRectangle(&brs, rt.left, rt.top, rt.Width(), rt.Height());
	// TODO: add draw code for native data here

	
	m_Tester->render_wrap(*m_backbufferGraphics);
	
	HBITMAP hBitmap;
	m_backbuffer->GetHBITMAP(Color::Red, &hBitmap);
	HDC hDC = m_backbufferGraphics->GetHDC();	
	HGDIOBJ obj = ::SelectObject(hDC, hBitmap);
	::BitBlt(*pDC, 0, 0, rt.Width(), rt.Height(), hDC, 0, 0, SRCCOPY);	
	::SelectObject(hDC, obj);
	::DeleteObject((HGDIOBJ)hBitmap);
	m_backbufferGraphics->ReleaseHDC(hDC);
}


// CUIEditor_CPPView printing
void CUIEditor_CPPView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CUIEditor_CPPView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CUIEditor_CPPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CUIEditor_CPPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CUIEditor_CPPView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CUIEditor_CPPView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CUIEditor_CPPView diagnostics

#ifdef _DEBUG
void CUIEditor_CPPView::AssertValid() const
{
	CView::AssertValid();
}

void CUIEditor_CPPView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CUIEditor_CPPDoc* CUIEditor_CPPView::GetDocument() const // non-debug version is inline
{
	//ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CUIEditor_CPPDoc)));
	return (CUIEditor_CPPDoc*)m_pDocument;
}
#endif //_DEBUG


// CUIEditor_CPPView message handlers


void CUIEditor_CPPView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == 1010)
	{
		InvalidateRect(NULL);
	}

	CView::OnTimer(nIDEvent);
}


int CUIEditor_CPPView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	this->SetTimer(1010, 1000/25, NULL);
	return 0;
}


void CUIEditor_CPPView::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	this->KillTimer(1010);
	delete m_window;
	delete m_Tester;
	delete m_backbuffer;
	delete m_backbufferGraphics;
	CView::OnClose();
}

LRESULT CUIEditor_CPPView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class
	m_window->process_message(message, wParam, lParam);

	return CView::WindowProc(message, wParam, lParam);
}


BOOL CUIEditor_CPPView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
	//return CView::OnEraseBkgnd(pDC);
}


void CUIEditor_CPPView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	if (m_backbuffer)
	{
		delete m_backbuffer;
		m_backbuffer = NULL;
	}

	if (m_backbufferGraphics)
	{
		delete m_backbufferGraphics;
		m_backbufferGraphics = NULL;
	}

	
#pragma push_macro("new")
#undef new
	m_backbuffer = new Bitmap(cx, cy);
#pragma pop_macro("new")
	m_backbufferGraphics = Graphics::FromImage(m_backbuffer);
}

void CUIEditor_CPPView::on_event(s_data_event& e)
{
	
}